package com.yunchao.library.socket.config

import com.yunchao.library.socket.enum.TransportType

// ================== 消息结构 ==================
data class Message(
    val header: MessageHeader,
    val content: MessageContent
)

data class MessageHeader(
    val bodyLength: Int,
    val direction: Int,
    val isPacket: Int,
    val indicator: Int,
    val sequence: Long,
    val transport: TransportType,
    val isChunked: Boolean
)

sealed class MessageContent {
    data class SingleMessage(val data: ByteArray) : MessageContent() {
        override fun equals(other: Any?) =
            other is SingleMessage && data.contentEquals(other.data)

        override fun hashCode() = data.contentHashCode()
    }


    data class ChunkedMessage(
        val chunks: MutableList<ByteArray>,  // 所有分块的列表
        val totalSize: Int            // 数据总大小（用于校验）
    ) : MessageContent() {
        override fun equals(other: Any?): Boolean {
            if (this === other) return true
            if (javaClass != other?.javaClass) return false
            other as ChunkedMessage
            return totalSize == other.totalSize &&
                    chunks.size == other.chunks.size &&
                    chunks.zip(other.chunks).all { (a, b) -> a.contentEquals(b) }
        }

        override fun hashCode(): Int {
            var result = chunks.hashCode()
            result = 31 * result + totalSize
            return result
        }
    }
}

